/**
 *  审核人查看某个公文的详细信息
 */
var doc_id;
var update_temp = false;
$(document).ready(function () {
    //获得参数的方法
    var request =
        {
            QueryString : function(val)
            {
                var uri = window.location.search;
                var re = new RegExp("" +val+ "=([^&?]*)", "ig");
                return ((uri.match(re))?(uri.match(re)[0].substr(val.length+1)):null);
            }
        }
    //调用方法获得参数
    doc_id = request.QueryString("doc_id");
    $.ajax({
        type: 'GET',
        url: '../document/review/power/' + doc_id,
        dataType: 'JSON',
        success: function (json,status){
            if(json.err_msg == null){
                if(json.data!=null){
                    $("#documentIframe").css("display","block");
                    $("#documentIframe").attr("src","../assets/js/document/pdfjs-dist/web/viewer.html?file=/document/review/read/" + doc_id);

                }else{
                    $("#draft_pdf").css("display","none");
                    $("#documentDown").parent().css("display","none");
                    $("#documentEdit").parent().css("display","none");
                }
            }else{
                alert(json.err_msg);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(textStatus);
        },
        fail: function () {
            console.log("fail!");
        },
    });

    //公文下载
    $("#documentDown").click(function () {
        $.ajax({
            type: 'GET',
            url: '../document/review/power/' + doc_id,
            dataType: 'JSON',
            success: function (json,status){
                window.location.href='../document/draft/down/' + doc_id;
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            },
            fail: function () {
            },
        })
    })

    //公文修改 公文上传
    $("#submitDocument").click(function () {
        var file = document.getElementById("upLoadFile");
        var file_io = new FormData();
        file_io.append("file",file.files[0]);
        file_io.append("confirm",0);
        $("#submitDocument").text("上传中 请勿关闭页面或重复提交!");
        $.ajax({
            type: 'post',
            url: '../document/review/edit/' + doc_id,
            dataType: 'JSON',
            data:file_io,
            processData: false,
            contentType : false,
            success: function (json,status){
                if(json.err_msg != null){
                    alert(json.err_msg);
                    $("#submitDocument").text("重新上传");
                }else{
                    $("#submitDocument").text("提交成功！");
                    $("#documentIframe").attr("src","../assets/js/document/pdfjs-dist/web/viewer.html?file=/document/review/read/" + doc_id);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log(textStatus);

            },
            fail: function () {
                console.log("fail!");
            },
        });
        cache: false
    })

    // 审核通过
    $("#passDocument").click(function () {
        var $option = $("#options").val();
        $.ajax({
            type: 'POST',
            url: '../document/review/pass',
            contentType: 'application/json;',
            dataType: 'JSON',
            data:JSON.stringify({
                "option":$option+" ",
                "doc_id":doc_id
            }),
            success: function (json,status){
                if(json.err_msg != null){
                    alert(json.err_msg);
                }else{
                    window.location.href= "../document/my_exam_doc.html"
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log(textStatus);
            },
            fail: function () {
                console.log("fail!");
            },
        })
    })

    // 审核不通过
    $("#failDocument").click(function () {
        var $option = $("#options2").val();
        $.ajax({
            type: 'POST',
            url: '../document/review/fail',
            contentType: 'application/json;',
            dataType: 'JSON',
            data:JSON.stringify({
                "option":$option+" ",
                "doc_id":doc_id
            }),
            //dataType: 'JSON',
            success: function (json,status){
                if(json.err_msg != null){
                    alert(json.err_msg);
                }else{
                    window.location.href= "../document/my_exam_doc.html"
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log(textStatus);
            },
            fail: function () {
                console.log("fail!");
            },
        })
    })

    /// 删除记录
    $("#deleteDocument").click(function () {
        $.ajax({
            type: 'DELETE',
            url: '../document/review/delete/' + doc_id,
            dataType: 'JSON',
            success: function (json,status){
                if(json.err_msg != null){
                    alert(json.err_msg);
                }else{
                    window.location.href= "../document/my_exam_doc.html"
                }
            },
            error: function (XMLHttpRequest, textStatus) {
                console.log(textStatus);
            },
            fail: function () {
                console.log("fail!");
            }
        })
    })



    // 获取所有附件列表
    $.ajax({
        dataType:'JSON',
        type: 'POST',
        url: '../document/review/annex/list/' + doc_id,
        dataType: 'JSON',
        success: function (json,status){
            console.log(json);
            if(json.err_msg != null){
                alert(json.err_msg);
            }else{
                var documentAnnex = json.data.documentAnnex;
                for(var i = 0;i<documentAnnex.length;i++){
                    $("#datatable").append("<tr>" +
                        "<td>"+documentAnnex[i].name+"</td>" +
                        "<td>"+documentAnnex[i].writer+"</td>" +
                        "<td><button type='button' class='btn btn-success' onclick='downFujian("+ documentAnnex[i].id +")'>下载</button>" +
                        "<button type='button' class='btn btn-primary' onclick='updateFujian(this,"+ documentAnnex[i].id +")'>修改</button>" +
                        "<button type='button' class='btn btn-warning' onclick='deleteFujian(" + documentAnnex[i].id + ")'>删除</button>" +
                        "<button type='button' class='btn btn-primary' data-toggle='modal' data-target='.bs-example-modal-bc'>上传新附件</button>" +
                        "</td>");
                }
                //console.log(json.data.documentAnnex);d
            }
        },
        error: function (XMLHttpRequest, textStatus) {
            console.log(textStatus);
        },
        fail: function () {
            console.log("fail!");
        }
    });

    // 审稿人上传新附件
    $("#addFujian2").click(function () {
        var file = document.getElementById("addFujian1");
        var file_io = new FormData();
        file_io.append("file",file.files[0]);
        //file_io.append("confirm",0);
        $.ajax({
            dataType:'JSON',
            type: 'POST',
            url: '../document/review/annex/add/' + doc_id,
            data:file_io,
            processData: false,
            contentType : false,
            success: function (json,status){
                //console.log(json);
                if(json.err_msg != null){
                    alert(json.err_msg);
                }else{
                    window.location.href = "../document/my_exam_content.html?doc_id="+doc_id;
                }
            },
            error: function (XMLHttpRequest, textStatus) {
                console.log(textStatus);
            },
            fail: function () {
                console.log("fail!");
            }
        });
    })

});

// 审核人删除某条附件
function deleteFujian(id) {
    if(confirm("是否要删除这条公文?")){
        $.ajax({
            dataType:'JSON',
            type: 'POST',
            url: '../document/review/annex/delete/' + doc_id + "/" + id,
            success: function (json,status){
                console.log(json);
                if(json.err_msg != null){
                    alert(json.err_msg);
                }else{
                    window.location.href = "../document/my_exam_content.html?doc_id="+doc_id;
                }
            },
            error: function (XMLHttpRequest, textStatus) {
                console.log(textStatus);
            },
            fail: function () {
                console.log("fail!");
            }
        });
    }

}


// 审核人修改某一个附件
function updateFujian(This,id) {
    if(update_temp == false){
        var parentNode = This.parentElement;
        var $parentNode = $(parentNode);
        $parentNode.append(
            "<input type='file' id='upLoadFile'>" +
            "<button type='button' class='btn btn-info btn-xs' onclick='update("+ id +")'>上传修改</button>");
        update_temp = true;
    }
}


// 修改附件
function update(id){
    var file = document.getElementById("upLoadFile");
    var file_io = new FormData();
    file_io.append("file",file.files[0]);
    //file_io.append("confirm",0);
    $.ajax({
        dataType:'JSON',
        type: 'POST',
        url: '../document/review/annex/update/' + doc_id + "/" + id,
        data:file_io,
        processData: false,
        contentType : false,
        success: function (json,status){
            //console.log(json);
            if(json.err_msg != null){
                alert(json.err_msg);
            }else{
                window.location.href = "../document/my_exam_content.html?doc_id="+doc_id;
            }
        },
        error: function (XMLHttpRequest, textStatus) {
            console.log(textStatus);
        },
        fail: function () {
            console.log("fail!");
        }
    });
}

//downFujian
function downFujian(id) {
    window.location.href="../document/review/annex/down/" + doc_id + "/" + id;
}
